import { deleteMemberAddressByIdAPI, getMemberAddressAPI } from '@/services/address'
import { useAddressStore } from '@/stores/modules/address'
import type { AddressItem } from '@/types/address'
import { onShow } from '@dcloudio/uni-app'
import { ref } from 'vue'

const useAddressHooks = () => {
  // 获取收货地址列表数据
  const addressList = ref<AddressItem[]>([])
  const getMemberAddressData = async () => {
    const res = await getMemberAddressAPI()
    addressList.value = res.result
  }

  // 删除收货地址
  const onDeleteAddress = (id: string) => {
    // 二次确认
    uni.showModal({
      content: '删除地址?',
      confirmColor: '#27BA9B',
      success: async (res) => {
        if (res.confirm) {
          // 根据id删除收货地址
          await deleteMemberAddressByIdAPI(id)
          // 重新获取收货地址列表
          getMemberAddressData()
        }
      },
    })
  }

  // 修改收货地址
  const onChangeAddress = (item: AddressItem) => {
    // 修改地址
    const addressStore = useAddressStore()
    addressStore.changeSelectedAddress(item)
    // 返回上一页
    uni.navigateBack()
  }

  // 初始化调用(页面显示)
  onShow(() => {
    getMemberAddressData()
  })

  return { addressList, onDeleteAddress, onChangeAddress }
}

export default useAddressHooks
